package com.cmcc.shared

import android.R
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.appcompat.app.SkinAppCompatDelegateImpl
import com.cmcc.shared.utils.LanguageUtil


abstract class BaseActivity : AppCompatActivity() {
    /***获取TAG的activity名称 */
    protected val TAG = this.javaClass.simpleName;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e(TAG, "当前所在Activity: "+TAG)
        initialize(savedInstanceState)
        initView(savedInstanceState)
        initEvent(savedInstanceState)
        initObserver(savedInstanceState)
        initStatus();
        SdkManager.logicHandler?.onPageStart(localClassName)

        //解决  华为手机虚拟按键
        //解决  华为手机虚拟按键
        if (AndroidWorkaround.checkDeviceHasNavigationBar(this)) {
            AndroidWorkaround.assistActivity(findViewById(R.id.content))
        }
    }

    /**
     * 换皮肤
     */
    override fun getDelegate(): AppCompatDelegate {
        return SkinAppCompatDelegateImpl.get(this, this)
    }
    /**
     * 初始化方法，用于初始化数据
     */
    abstract fun initialize(savedInstanceState: Bundle?)

    /**
     * 初始化方法，用户初始化视图
     */
    open fun initView(savedInstanceState: Bundle?) {}

    /**
     * 初始化方法，用于初始化事件
     */
    open fun initEvent(savedInstanceState: Bundle?) {}

    /**
     * 初始化方法，用于初始化观察对象
     */
    open fun initObserver(savedInstanceState: Bundle?) {}

    override fun attachBaseContext(newBase: Context?) {
        super.attachBaseContext(LanguageUtil.setLocal(newBase,SdkManager.getLanguage()))
    }

    override fun onDestroy() {
        SdkManager.logicHandler?.onPageEnd(localClassName)
        super.onDestroy()
    }

    /**
     *
     */
     fun initStatus() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            val flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
            val flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                val window = window
                val attributes = window.attributes
                attributes.flags = attributes.flags or flagTranslucentNavigation
                window.attributes = attributes
                getWindow().statusBarColor = Color.TRANSPARENT
            } else {
                val window = window
                val attributes = window.attributes
                attributes.flags =
                    attributes.flags or (flagTranslucentStatus or flagTranslucentNavigation)
                window.attributes = attributes
            }
        }


    }

}